import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Cookies from "js-cookie";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/',
    name: 'home',
    component: HomeView,
    redirect: '/index',
    children: [
      {
        path: 'index',//注意此处不能加 /
        name: "homePage",
        component: () => import('../views/home/index.vue')//动态加载小区管理页面
      },
      {//资产管理路由
        path:'property',
        name:'property',
        component: () => import("../views/property/index.vue"),
        children:[
          {
            //商业街
            path:'downtown',
            name:'downtown',
            component:() => import("../views/property/downtown.vue")
          },
          {
            //楼栋
            path:'building',
            name:'building',
            component:() => import("../views/property/building.vue")
          },
          {
            //店铺
            path:'house',
            name:'house',
            component:() => import("../views/property/house.vue")
          },
          {
            //租控图
              path:'rentControlChart',
              name:'rentControlChart',
              component:() => import("../views/property/rentControlChart.vue")
          }
        ]
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//全局路由守卫
router.beforeEach((to,from,next) => {
  if (to.path == '/login' || to.path == '/register') {
      next();
  } else {
  const username = Cookies.get("username");
  console.log(username)
  if (username == null || username == undefined || username == '') {
      router.push('/login');
  } else {
      next();
  }
  }
})


export default router
